// pages/business/myorderdetail/myorderdetail.js
var shihan = require('../../../common/fetch');
var config = require('../../../common/config');
var range;
var app = getApp();
var winScale = 750 / app.data.deviceInfo.windowWidth;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    showLoading: true,
    mask_flag: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var title = '', currentUrl = '', currentData = {};
    if (options.index == 1) {
      title = '订单详情';
      currentData["trade_no"] = options.tradeno;
      currentUrl = config.agent_trade_detail;
    } else if (options.index == 2) {
      title = '返货单详情';
      currentData["is_child"] = options.ischild;
      currentData["order_no"] = options.tradeno;
      currentUrl = config.agent_return_detail;
    }
    wx.setNavigationBarTitle({
      title: title,
    })
    this.setData({
      currentData: currentData,
      currentUrl: currentUrl,
      currentIndex: options.index
    })
    var windowres = wx.getSystemInfoSync();
    range = (windowres.windowHeight / 2) - (270 / winScale) + 600 / winScale;
    var animation = wx.createAnimation({
      duration: 350,
      timingFunction: 'ease-in-out',
    });
    this.animation = animation;
    this.refreshDetail();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {
    if (this.data.isfresh) {
      var pages = getCurrentPages();
      if (pages.length >= 2) {
        var prevPage = pages[pages.length - 2];
        prevPage.setData({
          isfresh: true
        })
      }
      this.setData({
        showLoading: true
      })
      this.refreshDetail();
    }
  },
  /**
   * 页面事件
   */
  refreshDetail: function() {
    var that = this;
    shihan.sendRequest(that.data.currentUrl, 'POST', that.data.currentData)
      .then(function(response) {
        if (response.data.code == 0) {
          // response.data.data.sh_is_top_agent = 0;
          // response.data.data["sh_button_btn"] = {
          //   "sh_cancel_btn": "0",
          //   "sh_view_logistic_btn": "0",
          //   "sh_add_logistic_btn": "0",
          //   "sh_send_btn": "0",
          //   "sh_confirm_btn": "0",
          //   "sh_agree_btn": "1",
          //   "sh_refuse_btn": "1"
          // }
          that.setData({
            showLoading: false,
            order_detail: response.data.data,
          })
        }
        wx.stopPullDownRefresh();
      }, function(error) {
        console.log(error);
      });
  },
  copyContent: function(e) {
    var that = this
    wx.setClipboardData({
      data: String(e.currentTarget.dataset.content),
      success: function(res) {
        wx.getClipboardData({
          success: function(res) {
            if (res.data) {
              console.log('复制成功')
            }
          }
        })
      }
    })
  },
  addLogistic: function(e) { //添加物流信息
    wx.navigateTo({
      url: '/pages/business/addlogistic/addlogistic?index=' + e.currentTarget.dataset.index +'&orderid=' + e.currentTarget.dataset.orderid,
    })
  },
  lookLogistic: function(e) { //查看物流信息
    wx.navigateTo({
      url: '/pages/other/viewlogistics/viewlogistics?index=' + e.currentTarget.dataset.type+'&id=' + e.currentTarget.dataset.orderid,
    })
  },
  sweepCode: function(e) { //扫码发货，收货
    wx.navigateTo({
      url: '/pages/business/sweepcode/sweepcode?orderid=' + e.currentTarget.dataset.orderid + '&type=' + e.currentTarget.dataset.type + '&index=' + e.currentTarget.dataset.index,
    })
  },

  cancelReturn: function (e) {//我的返货单列表-取消申请
    var url = config.agent_return_cancel;
    var word = "取消申请";
    this.operateFn(e, url, word);
  },

  refuseReturn: function (e) {
    var url = config.agent_return_verify;
    var word = "拒绝返货";
    this.operateFn(e, url, word);
  },

  agreeReturn: function (e) {
    var url = config.agent_return_verify;
    var word = "同意返货";
    this.operateFn(e, url, word);
  },

  operateFn: function (e, url, word) {
    var that = this, data = {},
      orderno = e.currentTarget.dataset.orderid,
      status = e.currentTarget.dataset.status ? e.currentTarget.dataset.status : '';
    if (status) {
      data["status"] = status;
      data["order_no"] = orderno;
    } else {
      data["order_no"] = orderno;
    }
    wx.showModal({
      title: "提示",
      content: '确定要' + word +'吗？',
      confirmText: "确认",
      confirmColor: "#c40000",
      showCancel: true,
      success: function (res) {
        if (res.confirm) {//agent_return_cancel
          shihan.sendRequest(url, 'POST', data)
            .then(function (response) {
              if (response.data.code == 0) {
                shihan.showToast.call(that, '操作成功');
                that.refreshDetail();
                var pages = getCurrentPages();
                if (pages.length >= 2) {
                  var prevPage = pages[pages.length - 2];
                  prevPage.setData({
                    isfresh: true
                  })
                }
              } else {
                shihan.showToast.call(that, response.data.msg);
              }
            }, function (error) {
              console.log(error);
            });
        }
      }
    })
  },

  lookQuestion: function (e) {
    this.animation.translateY(-range).step();
    this.setData({
      mask_flag: true,
      animationData: this.animation.export()
    })
  },
  closeMask: function () {
    this.animation.translateY(range).step();
    this.setData({
      mask_flag: false,
      animationData: this.animation.export()
    })
  },

  returnPolicy: function (e) {
    wx.navigateTo({
      url: '/pages/strategyinfo/strategyinfo?share=0&nologin=1&url=' + encodeURIComponent(e.currentTarget.dataset.url)
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    this.refreshDetail();
  }
})